// 改名时候
import { gptReply } from "../utils/reply.js"
export async function onRoomTopic(room, topic, oldTopic, changer) {
  try {
    console.log('Bot', 'EVENT: room-topic - Room "%s" change topic from "%s" to "%s" by member "%s"',
      room,
      oldTopic,
      topic,
      changer,
    )
    oldTopic = await room.payload.topic || oldTopic
    await room.say(`@${changer.name()}把群聊“${oldTopic}”改名为“${topic}”`)
  } catch (e) {
    console.log('Bot', 'room-topic event exception: %s', e.stack)
  }
}
// 群聊有人加入时候
export async function onRoomJoin(room, inviteeList, inviter) {
  console.log(111)
  console.log(room)
  console.log(room.say)

  try{
    console.log('Bot', 'EVENT: room-join - Room "%s" got new member "%s", invited by "%s"',
    
    )
     let roomName = room.payload.topic
      let inviterName =   inviter.name({})
    const list = inviteeList.map(c => c.name({})).join('、')
  let says = await gptReply(`欢迎成员 ${list}入群`,room)
  room.say(says)
  }catch(e){
    console.log(e)
  }
  // let says = await gptReply(`欢迎成员 ${list}加入群${topic}`)
  // console.log(says)
  // await room.say` @${list} ${says}`
}

export async function onRoomLeave(room, leaverList) {
  console.log('Bot', 'EVENT: room-leave - Room "%s" lost member "%s"',
    await room.topic(),
    leaverList.map(c => c.name()).join(','),
  )
  const topic = await room.topic()
  const name = leaverList[0] ? leaverList[0].name() : 'no contact!'
  await room.say(`群成员"${name}"离开了群聊 "${topic}"!`)
}

export async function onRoomInvite(room, inviteeList, inviter) {
  console.log('Bot', 'EVENT: room-join - Room "%s" got new member "%s", invited by "%s"',
    await room.topic(),

    inviter.name(),
  )
  const list = inviteeList.map(c => c.name()).join(',')
  console.log('inte')
  console.log(list)
  // const topic = await room.topic()
  // await room.say ` @${list} ${says}`
}